#!/bin/sh
# Search the image in the local disks
PREREQ="tmpfs"

prereqs()
{
	echo "$PREREQ"
}

case $1 in
# get pre-requisites
prereqs)
	prereqs
	exit 0
	;;
esac

for fs in isofs ext2 ext3 vfat ; do
        modprobe -q $fs >> /tmp/initramfs.debug 2>&1
done

mkdir -p /mnt/dev >> /tmp/initramfs.debug 2>&1

mkdir -p /mnt/changes >> /tmp/initramfs.debug 2>&1

found=n

test="${dirconf}"

while [ ${found} = n ]
do
for sd in sda1 sdb1 hda hdb hdc hdd hde hdf sdc1 sdd1 sde1 sdf1 sdg1 sdh1 sdi1 sdj1 sdk1 sdl1 scd0 scd1; do
        mount -t auto /dev/$sd /mnt/dev >> /tmp/initramfs.debug 2>&1
        if [ -e /mnt/dev/*.squashfs ]; then

                found=y

            if [ "x$test" = "xram"  ]; then

               mkdir -p /mnt/ram
               
               echo "
Copiant fitxers a la memoria RAM ...
"
               cp -dfR /mnt/dev/* /mnt/ram/
              
                umount /mnt/dev >> /tmp/initramfs.debug 2>&1

#               echo "1" > /proc/sys/dev/cdrom/autoeject
             
             cdrom=$(grep $sd /proc/sys/dev/cdrom/info)

             if [ "x$cdrom" != "x" ]; then

             /usr/bin/eject -pm /dev/$sd

             read -p  "Ja pots retirar el CD i clicar enter per continuar." respostacd

             /usr/bin/eject -pT /dev/$sd

             else

             read -p  "Ja pots retirar la andromina USB i clicar enter per continuar." respostausb

	     fi
             
              
        break
   
        fi

        else
                umount /mnt/dev >> /tmp/initramfs.debug 2>&1
        fi

done

done

test="${dirconf}"

if [ "x$test" != "x"  ] && [ "x$test" != "xram" ]; then



for sd in sda2 sdb2 sdc2 sdd2 sde2 sdf2 sdg2 sdh2 sdi2 sdj2 sdk2 sdl2; do
        mount -t ext2 -o rw /dev/$sd /mnt/changes >> /tmp/initramfs.debug 2>&1
        if [ -e /mnt/changes/.flag ]; then
	mkdir -p /mnt/changes/${dirconf} >> /tmp/initramfs.debug 2>&1
		break
        else
                umount /mnt/changes >> /tmp/initramfs.debug 2>&1
        fi
done
fi

if [ ${found} = y ]; then
	echo "LOCAL: image mounted" >> /tmp/initramfs.debug
	exit 0
else
	echo "LOCAL: ERROR -> image non mounted" >> /tmp/initramfs.debug
	exit 1
fi





